/*
  This file is part of jTopaz.
  http://gcl.cis.udel.edu/projects/jtopaz
  Copyright (C) 2009 University of Delaware

  jTopaz is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  jTopaz is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of                                                        
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with jTopaz.  If not, see <http://www.gnu.org/licenses/>.
*/
var username;
var passwd;
var myProxyServer;
var client;
var listContents;
var transferCount = 0;
var lastUpdated=0;
var infoClientStarted=false;
var transferClientStarted=false;
var uploadClientStarted=false;
var loginDone=false;
var localDepth=0;
var remoteDepth=0;
var currentLocalDir;
var pimpMyDl=true;
var serverActive=true;

function remoteDelete() {
    var index = getSelectedIndex("remoteTree");
	
    if(index == -1) {
	alert("no file selected");
	return;
    }
	
    //get the path and filename
    var filePath = getSelectedValue("remoteTree", index);
    var fileName = getSelectedText("remoteTree", index);
	
    var element = document.getElementById(filePath);
    var dir=false;
    if (element.getAttribute("src") == "chrome://sample/content/folder.gif") 
	dir=true;
	
    //load the component	
    var applet = window.frames.appletFrame;
    applet.delete(filePath, dir);
    clear("remoteContents");
    buildTree("remoteContents", false);
}

function localDelete() {
    var index = getSelectedIndex("localTree");
    if(index == -1) {
	alert("no file selected");
	return;	
    }
    var filePath = getSelectedValue("localTree", index);
    var fileName = getSelectedText("localTree", index);

    var file = Components.classes["@mozilla.org/file/local;1"]
	.createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(filePath);
    file.remove(true);
    clear("localContents");
    var dirPath = filePath.replace(fileName, "");

    var localContents = document.getElementById("localContents");
    buildLocalTree(dirPath, localContents);
}

function changePath() {
    //var textBox = document.getElementById("remotePath");

    //load the applet
    //var applet = window.frames.appletFrame;

    //applet.changeDir(textBox.value);
    
    clear("remoteContents");

    var remoteContents = document.getElementById("remoteContents");
    buildTree(remoteContents, true);
    remoteDepth = 0;
}

function changeRemoteDir() {
    var theIndex = getSelectedIndex("remoteTree");
	
    var path = getSelectedValue("remoteTree", theIndex);
	
    var element = document.getElementById(path);
	
	
    if (element.getAttribute("src") != "chrome://sample/content/folder.gif") {
	return;
    }
	
    //load the applet
    var applet = window.frames.appletFrame;

    applet.changeDir(path);
    
    clear("remoteContents");
    var remoteContents = document.getElementById("remoteContents");
    
    buildTree(remoteContents, false);
    remoteDepth = remoteDepth + 1;
    
    var upButton = document.getElementById("remoteUp");
    
    if (upButton.getAttribute("disabled") == "true") {
    	upButton.setAttribute("disabled", "false");
    }  	
}

function goUpRemoteDir() {
	
    if (remoteDepth == 0) {
	return;
    }
	
    //load the applet
    var applet = window.frames.appletFrame;

    
    applet.goUpDir();
    
    clear("remoteContents");
    var remoteContents = document.getElementById("remoteContents");
    
    buildTree(remoteContents, false);
    
    remoteDepth = remoteDepth - 1;
    
    if(remoteDepth == 0) {
    	var upButton = document.getElementById("remoteUp");
    	upButton.setAttribute("disabled", "true");	
    }	
}
function changeLocalDir() {
    var theIndex = getSelectedIndex("localTree");
    //alert(theIndex);
    var path = getSelectedValue("localTree", theIndex);
	
    var element = document.getElementById(path);
	
	
    if (element.getAttribute("src") != "chrome://sample/content/folder.gif") {
	return;
    }
	
    clear("localContents");
    var localContents = document.getElementById("localContents");
    //alert(localContents);
    buildLocalTree(path, localContents);
    localDepth = localDepth + 1;
    currentLocalDir = path;	
	
    var upButton = document.getElementById("localUp");
	
    if (upButton.getAttribute("disabled") == "true") {
	upButton.setAttribute("disabled", "false");	
    }
}

function goUpLocalDir() {
	
    if (localDepth == 0) {
	return;
    }
    var current = Components.classes["@mozilla.org/file/local;1"]
	.createInstance(Components.interfaces.nsILocalFile);
    current.initWithPath(currentLocalDir);
    
    var parentDir = current.parent;
    
    clear("localContents");
    var localContents = document.getElementById("localContents");
    buildLocalTree(parentDir.path, localContents);
    localDepth = localDepth - 1;
    if (localDepth == 0) {
	var upButton = document.getElementById("localUp");
	upButton.setAttribute("disabled", "true");	
    }
    currentLocalDir = parentDir.path;
	
}

/*
  given a tree name, returns the actual tree in the document
*/
function getTree(treeName) {
    var tree = document.getElementById(treeName);
    return tree;
}

/*
  given a tree name, returns the index that is currently selected.
*/
function getSelectedIndex(treeName) {
    var tree = getTree(treeName);
    var index = tree.view.selection.currentIndex;
    return index;
}

/*
  given a tree, returns the value of the currently selected element
*/
function getSelectedValue(treeName, index) {
    var tree = getTree(treeName);
    try {
	var value = tree.view.getCellValue(index, tree.columns.getFirstColumn());
	return value;
    }
    catch(e) {

    }
}

/*
  given a tree, returns the text of the currently selected index
*/
function getSelectedText(treeName, index) {
    var tree = getTree(treeName);
    return tree.view.getCellText(index, tree.columns.getFirstColumn());
}

/*
  takes a DOM element name, removes all of it's children
*/
function clear(dest) {
    var destElement = document.getElementById(dest);
	
    if (destElement == null) 
	return;
		
    var nodes = destElement.childNodes;
	
    while(destElement.hasChildNodes()){
	destElement.removeChild(nodes[0]);	
    }
}

/*
  displays the currently selected elements contents in it's respective current tree
*/
function displaySelection(selectionSource, destString) {
	
    //get the currently selected inde
    var index = getSelectedIndex(selectionSource);
	
    var location = getSelectedValue(selectionSource, index);
	
    //clear the current contents
    clear(destString);
	
    //get the dest element
    var dest = document.getElementById(destString);	
	
    //if it's the local tree
    if((selectionSource == "localTree") || (selectionSource == "currentLocalTree")) {
		
	//create the directory from the selected index
	var directory = Components.classes["@mozilla.org/file/local;1"]
	    .createInstance(Components.interfaces.nsILocalFile);
	directory.initWithPath(location);
    
	//make sure the file actually exists
    	if (directory.exists()) {
    	
	    //make sure it's a directory
	    if (directory.isDirectory()) {
    			
    			
		var enumerator = directory.directoryEntries;
    		
		//loop through the contents
		while(enumerator.hasMoreElements()){
		    var next = enumerator.getNext();
		    var currentFile = next.QueryInterface(Components.interfaces.nsIFile);
    			
		    item = document.createElement("treeitem");
		    var row = document.createElement("treerow");
		    var cell = document.createElement("treecell");
		    cell.setAttribute("id", currentFile.path);
		    cell.setAttribute("label", currentFile.leafName);
		    cell.setAttribute("value", currentFile.path);
		    if(currentFile.isDirectory()) {
			cell.setAttribute("src", "chrome://sample/content/folder.gif");	
		    }
		    row.appendChild(cell);
    		
		    item.appendChild(row);						dest.appendChild(item);
    				
		}	
	    }	
    	}
    }
	
	
    //if it's the remote tree
    else {
		
	//load the component
	var applet = window.frames.appletFrame;                       
	applet.update();
	applet.changeDir(location);
		
	buildTree(dest, true);
    }
}

/*
  called on double click of an element in the current trees
*/
function doAction(scope) {
	
    //if you're in the local contents
    if (scope == "local") {
	var index = getSelectedIndex("currentLocalTree");
	var filePath = getSelectedValue("currentLocalTree" ,index);	
		
	var file = Components.classes["@mozilla.org/file/local;1"]
	    .createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath(filePath);
		
	//if the selected file is a directory
	if(file.isDirectory()) {
	    //display it
	    displaySelection("currentLocalTree", "currentLocalContents");
	}
	else {
	    //otherwise upload
	    //alert("upload " + filePath);
	}
    }
	
    //otherwise if you're in the remote contents
    else {
		
	//can check if it's a directory, by seeing if an element already exists with it?
	//this might be wrong, might have to change it
	var index = getSelectedIndex("currentRemoteTree");
	var filePath = getSelectedValue("currentRemoteTree", index);
	var dir = document.getElementById(filePath);
		
	//display the directory
	if(dir != null) {
	    displaySelection("currentRemoteTree", "currentRemoteContents");
	}
		
	//download
	else {
	    download();
	}
    }
}

function togglePimped(){
    pimpMyDl=!pimpMyDl;
}

function toggleServerActive(){
    serverActive=!serverActive;
}

/*
  starts a download
*/
function download() {
	
    var index = getSelectedIndex("remoteTree");
	
    if(index == -1) {
	alert("no file selected");
	return;
    }
	
    //get the path and filename
    var filePath = getSelectedValue("remoteTree", index);
    var fileName = getSelectedText("remoteTree", index);

    var localFile = currentLocalDir + "/" + fileName;
	
    //load the component	
    var applet = window.frames.appletFrame;
	
    
    //get the status of the client
    //var result = applet.getTransferClientStatus();
    result = "success";
    //check to make sure you can download
    if (result == "success") {
    	//create a queue entry
	createQueueRow("download", filePath, localFile, fileName);
   
	applet.update();
	//add the job to the queue
	pimpMyDl = document.getElementById("phoebus").hasAttribute("checked");
	serverActive = document.getElementById("serveractive").hasAttribute("checked");

	if(pimpMyDl){
	    applet.addToQueue("downloadp", filePath, localFile);
	}
	else if(!pimpMyDl) {
	    //var serverActive=confirm("Initiate connection from server?");
	    if(serverActive){
		applet.addToQueue("downloadS", filePath, localFile);
	    }
	    else{
		applet.addToQueue("download", filePath, localFile);
	    }
	}

	return;	
    }
}

/*
  upload method
*/
function upload() {
	
    //load the component
    var applet = window.frames.appletFrame;
                                    
    //get the index
    var index = getSelectedIndex("localTree");
	
    if(index == -1) {
	alert("no file selected");
	return;
    }	
	
	
    var filePath = getSelectedValue("localTree", index);
    var fileName = getSelectedText("localTree", index);
	
    var element = document.getElementById(filePath);
	
    var dir;
    //alert(element.getAttribute("src"));
    if (element.getAttribute("src") == "chrome://sample/content/folder.gif") {
	//alert("uploading a directory");
	dir = true;
	//var remoteDir = applet.getCurrentDir() + fileName;
	applet.update();
	var result = applet.makeDir(fileName);
	var remoteDir = applet.getCurrentDir() + "/" + fileName;
	//alert("make dir? " + result);		
    }
	
    else {
	dir = false;
	var remoteDir = applet.getCurrentDir();
    }
	
                                    
	
    /*
      index = getSelectedIndex("remoteTree");
	
      if(index == -1) {
      alert("no destination selected");
      return;
      }
	
      //create the fullpath of the new file
      var remoteDir = getSelectedValue("remoteTree", index);*/
	
    if (! dir) {
	var remoteFile = remoteDir + "/" + fileName;
	
	//create the source file
	var sourceFile = new java.io.File(filePath);
	
    
    	//add the job to the queue                      
	applet.addToQueue("upload", filePath, remoteFile);
	
	//create a queue row
	createQueueRow("upload", sourceFile.getPath(), remoteFile, sourceFile.getName());
	
	return;	
    }
	
    else {
	var directory = Components.classes["@mozilla.org/file/local;1"]
	    .createInstance(Components.interfaces.nsILocalFile);
        directory.initWithPath(filePath);
        
        var contents = directory.directoryEntries;
        
        while (contents.hasMoreElements()) {
        	
	    var next = contents.getNext();
	    var currentFile = next.QueryInterface(Components.interfaces.nsIFile);
        	
	    //alert("upload: " + currentFile.path);
	    if (currentFile.leafName[0] == ".") {
		//do nothing	
	    }
	    else if (!currentFile.isDirectory()) {
        		
		var sourceFile = new java.io.File(currentFile.path);
		//alert(remoteDir);
		var remoteFile = remoteDir + "/" +  currentFile.leafName;
		//applet.addToQueue("upload", currentFile.path, remoteFile);
        		
		createQueueRow("upload", sourceFile.getPath(), remoteFile, sourceFile.getName());
	    }
        	
	    else {
		//alert("recurse");	
	    }
        }
    }
}

/*
  create a new row in the queue
*/
function createQueueRow(type, transferFile, otherFile, name) {


	
    //get the queue rows
    var rows = document.getElementById("downloadRows");
	
    var item = document.createElement("treeitem");
    item.setAttribute("id", name + " downloadItem");
	
    if (type == "download") {
	item.setAttribute("value", "type: " + type + " localFile: " + otherFile + " remoteFile: " + transferFile + " transferCount: " + transferCount);
    }
    else if (type == "upload") {
	item.setAttribute("value", "type: " + type + " localFile: " + transferFile + " remoteFile: " + otherFile + " transferCount: " + transferCount);	
    }
    var row = document.createElement("treerow");
    row.setAttribute("id", name + " downloadRow");
    //alert(transferCount);
    row.setAttribute("value", transferCount);
    //transferCount = transferCount + 1;
	
    var label1 = document.createElement("treecell");
    label1.setAttribute("id", name + " label1");
    label1.setAttribute("label", name);
	
    var label2 = document.createElement("treecell");
    label2.setAttribute("id", name + " label2");
    label2.setAttribute("label", "Queued");
	
    var label3 = document.createElement("treecell");
    label3.setAttribute("id", name + " label3");
	
    //create a progress meter
    var progressMeter = document.createElement("treecell");
    progressMeter.setAttribute("id", name + " meter");
    progressMeter.setAttribute("mode", "normal");
    progressMeter.setAttribute("value", 0);
	
    row.appendChild(label1);
    row.appendChild(label2);
    row.appendChild(label3);
    row.appendChild(progressMeter);
    item.appendChild(row);
    rows.appendChild(item);
	
    var tree = document.getElementById("transferTree");
    tree.view.setCellValue(tree.view.rowCount-1, tree.columns.getFirstColumn(), transferCount);
    transferCount = transferCount+1;

}

/*
  builds the remote tree.  takes the parent element, and the currentlySelected dir.  gets the contents and appends it 
*/

function buildTree(parent, currentlySelected) {
	
    //load the component.
    var applet = window.frames.appletFrame;
     
     
    //update the info client                               
    applet.update();

    var textBox = document.getElementById("remotePath");
    if ((textBox.value != "remote path") && currentlySelected) {
	applet.changeDir(textBox.value);	
    }
    	
    //get the contents
    applet.list();
	
    loading();
    waitForContents();
	
}

function waitForContents() {
	
    var applet = window.frames.appletFrame;
	
    var complete = applet.listComplete();
	
    var status = applet.getListStatus();
    if (complete) {
	if (status == "success") {
	    //alert("build the tree");
	    removeLoading();
	    listRemoteContents();	
	}
	else {
	    alert("getting contents failed");
	    alert(status);	
	}
    }
    else {
	setTimeout("waitForContents()", 1000);
    }
}

function listRemoteContents() {
    
    var applet = window.frames.appletFrame;
    
    var parent = document.getElementById("remoteContents");
    var files = applet.getContents();
    //loop through the files
        if (files == null) {
	alert("error getting remote contents");
	disconnect();	
    }
    for(var i=0; i<files.length; i++) {
	
	var value = applet.getCurrentDir() + "/" + files[i].getFileName();
	
	var item = document.createElement("treeitem");
	item.setAttribute("value", value);
	var row = document.createElement("treerow");
	row.setAttribute("value", value);
	var cell1 = document.createElement("treecell");
	var cell2 = document.createElement("treecell");
	var cell3 = document.createElement("treecell");
	
	if (files[i].get("type") == "file") {
	    
	    cell1.setAttribute("label", files[i].getFileName());
	    cell1.setAttribute("value", value);
	    cell1.setAttribute("id", value);
	    
	    var size = files[i].get("size");
	    //alert("setting id as " + value + " remote size");
	    cell2.setAttribute("id", value + " remote size");
	    cell2.setAttribute("value", size);
	    if (size >= 1073741824) {
		size = size/1073741824;
		size = size*100;
		size = Math.floor(size);
		size = size/100;
		cell2.setAttribute("label", size + " GB");	
	    }
	    
	    else if (size >= 1048576) {
		size = size/1048576;
		size = size * 100;
		size = Math.floor(size);
		size = size/100;
		cell2.setAttribute("label", size + " MB");	
	    }
	    
	    else {
		size = size/1024;
		size = size * 100;
		size = Math.floor(size);
		size = size/100;
		cell2.setAttribute("label", size + " kB");	
	    }
	    
	    //alert(files[i].get("date"));
	    
	    row.appendChild(cell1);
	    row.appendChild(cell2);
	    row.appendChild(cell3);
	    item.appendChild(row);
	    parent.appendChild(item);
	}
	
	else if (files[i].get("type") == "dir") {
	    
	    applet.update();
	    
	    item.setAttribute("id", "remote item " + value);
	    
	    cell1.setAttribute("label", files[i].getFileName());
	    cell1.setAttribute("id", value);
	    cell1.setAttribute("value", value);
	    cell1.setAttribute("src", "chrome://sample/content/folder.gif");
	    
	    cell2.setAttribute("label", "-");
	    cell2.setAttribute("id", value + " remote size");
	    cell2.setAttribute("value", 0);
	    row.appendChild(cell1);
	    row.appendChild(cell2);
	    item.appendChild(row);
	    parent.appendChild(item);
	    //alert(files[i].get("modify"));
	}
	
	var dateString = files[i].get("modify");
	var year = dateString.substring(0, 4);
	var month = dateString.substring(4,6);
	
	var day = dateString.substring(6,8);
	
	var hour = dateString.substring(8,10);
	
	var minute = dateString.substring(10,12);
	
	var second = dateString.substring(12,14);
	
	var cal = new java.util.GregorianCalendar();
	cal.set(year, month, day, hour, minute, second);
	
	cell3.setAttribute("label", cal.getTime());
	cell3.setAttribute("id", value + " remote date");
	//alert("set id as " + cell3.getAttribute("id"));
	cell3.setAttribute("value", cal.getTimeInMillis());
	//alert(files[i].getFileName());
	row.appendChild(cell3);			
    }	
    
    
}

function disconnect() {

    var applet = window.frames.appletFrame;
    applet.disconnect();

    clear("remoteContents");
    clear("localContents");
	
    document.getElementById("disconnect").setAttribute("disabled", true);
	
    document.getElementById("connect").setAttribute("disabled", false);	
	
    document.getElementById("download").setAttribute("disabled", true);
	
    document.getElementById("upload").setAttribute("disabled", true);
	
}


function clearTransfers() {
    var rows = document.getElementById("downloadRows");
    var children = rows.childNodes;
	
    for (var i=0; i<children.length; i++) {
	var info = children[i].getAttribute("value");
	var element = children[i].childNodes[0];
	var transferStatus = element.childNodes[1].getAttribute("label");
	if ((transferStatus == "Cancelled") || (transferStatus == "complete")) {
	    //alert("Clear " + element.getAttribute("id"));
	    children[i].parentNode.removeChild(children[i]);
	    //hack if you remove one decrement i
	    i--;
	}	
    }

}
function cancel() {

    var index = getSelectedIndex("transferTree");
	
    var text = getSelectedText("transferTree", index);
    //alert(text);
    var value = getSelectedValue("transferTree", index);
    //alert(value);

    var rows = document.getElementById("downloadRows");
	
    var children = rows.childNodes;

    for (var i=0; i<children.length; i++) {
	var info = children[i].getAttribute("value");
	var element = children[i].childNodes[0];
	//alert(element.getAttribute("id"));
	//alert(element.getAttribute("value"));
	if (element.getAttribute("id") == text + " downloadRow") {
		
	    if(element.getAttribute("value") == value) {
			
		//alert("cancel this one");	
				
		var applet = window.frames.appletFrame;
		var status = element.childNodes[1];
	
		if (status.getAttribute("label") != "complete") 
		    status.setAttribute("label", "Cancelled");
		applet.cancel(info);
	    }
				
	}	
    }	

}

function localNameSort() {
}

function localSizeSort() {
    Sort("local", "size");	
}

function localDateSort() {
    Sort("local", "date");	
}

function remoteSizeSort() {
    Sort("remote", "size");	
}

function remoteDateSort() {
    Sort("remote", "date");	
}

function Sort(tree, type) {
    if(tree == "local") {
	var rows = document.getElementById("localContents").childNodes;

	var sorted = new Array();
	var parent = rows[0].parentNode;
	for(var i=0; i<rows.length; i++) {
	    sorted[i] = rows[i];
	}
				
	if (type == "size")
	    sorted.sort(localSizeCompare);
	else if(type == "date")
	    sorted.sort(localDateCompare);
	else if(type == "name")
	    sorted.sort(localNameCompare);
	for(var i=0; i<sorted.length; i++) {
	    parent.appendChild(sorted[i]);
	}			
    }
	
    if(tree == "remote") {
	var rows = document.getElementById("remoteContents").childNodes;
	var sorted = new Array();
	var parent = rows[0].parentNode;
		
	for(var i=0; i<rows.length; i++) {
	    sorted[i] = rows[i];	
	}
	if (type == "size")
	    sorted.sort(remoteSizeCompare);
	else if (type == "date")
	    sorted.sort(remoteDateCompare);
	for (var i=0; i<sorted.length; i++) {
	    parent.appendChild(sorted[i]);	
	}
    }
}

function localSizeCompare(a, b) {
    var elementA = document.getElementById(a.getAttribute("value") + " local size");
    var elementB = document.getElementById(b.getAttribute("value") + " local size");
    var valA = elementA.getAttribute("value");
    var valB = elementB.getAttribute("value");
    return valA - valB;
}

function localDateCompare(a, b) {
    var elementA = document.getElementById(a.getAttribute("value") + " local date");
    var elementB = document.getElementById(b.getAttribute("value") + " local date");
    var valA = elementA.getAttribute("value");
    var valB = elementB.getAttribute("value");
    return valA - valB;
}	

function remoteSizeCompare(a, b) {
    var elementA = document.getElementById(a.getAttribute("value") + " remote size");
    var elementB = document.getElementById(b.getAttribute("value") + " remote size");
    var valA = elementA.getAttribute("value");
    var valB = elementB.getAttribute("value");
    return valA - valB;
}

function remoteDateCompare(a, b) {
    var elementA = document.getElementById(a.getAttribute("value") + " remote date");
    if(!elementA)
	alert("element a is null");
    var elementB = document.getElementById(b.getAttribute("value") + " remote date");
    if(!elementB)
	alert("element b is null");
    var valA = elementA.getAttribute("value");
    var valB = elementB.getAttribute("value");
    return valA - valB;
}	

function localNameCompare(a, b) {
    var elementA = document.getElementById(a.getAttribute("value"));
    var elementB = document.getElementById(b.getAttribute("value"));
	
    var valA = elementA.getAttribute("label");
    alert(valA);
    var javaInt = new java.lang.Integer(valA);
    var valB = parseInt(elementB.getAttribute("label"));
    alert(javaInt);
    return valA - valB;
}

function selectionSort(values, leng, type, newArray) {
    if(leng == 1)
	return sorted;
	
    var current = document.getElementById(values[0].getAttribute("value") + " local size");
    var max = current.getAttribute("value");
    alert(leng);
    alert("max before: " + max);
    var maxIndex = 0;
    var i;
	
    for(i=1; i<leng; i++) {
	if(values[i] == null) {
	    alert("index " + i + " is now null");	
	}
	else {
	    current = document.getElementById(values[i].getAttribute("value") + " local size");
	    var temp = current.getAttribute("value");
	    //alert("current " + current.getAttribute("value"));
	    //alert("max " + max);
	    var diff = temp - max;
	    if(diff > 0) {
		//alert("current is greater");
		maxIndex = i;
		max = current.getAttribute("value");
	    }
	}
    }
    newArray[leng-1] = values[maxIndex];
	
    values = selectionSort(values, leng-1, type);
    return values;
}
function quickSort(values, leng, type) {
    //alert("in quickSort?");
    var pivot;
    if(leng < 1)
	return values;
    //just make the pivot the middle element
    if((leng % 2) == 0)
	pivot = leng/2;
    else
	pivot = (leng-1)/2
		
	    var less = new Array();
    var greater = new Array();
	
    var i;
    var lIndex=0;
    var gIndex=0;
    var temp;
    var pivotValue = document.getElementById(values[pivot].getAttribute("value") + " local size").getAttribute("value");
    //alert(pivotValue);
    for(i=0; i<pivot; i++) {
	temp = document.getElementById(values[i].getAttribute("value") + " local size");
	//alert(temp.getAttribute("value"));
	//alert(pivotValue);
	if(temp.getAttribute("value") <= pivotValue) {
	    less[lIndex] = values[i];
	    lIndex++;	
	}
	else {
	    //alert("greater");
	    greater[gIndex] = values[i];
	    gIndex++;	
	}	
    }
    for(i=pivot+1; i<values.length; i++) {
	temp = document.getElementById(values[i].getAttribute("value") + " local size");
	if(temp.getAttribute("value") <= pivotValue) {
	    less[lIndex] = values[i];
	    lIndex++;	
	}
	else {
	    //alert("greater");
	    greater[gIndex] = values[i];
	    gIndex++;	
	}	
		
    }
    less = quickSort(less, less.length, type);
    greater = quickSort(greater, greater.length, type);
    //less[less.length] = pivot;
    for(i=0; i<less.length; i++) {
	values[i] = less[i];	
    }
    for(i=0; i<greater.length; i++) {
	values[pivot+i+1] = greater[i];	
    }
    for(i=0; i<values.length; i++) {
	//alert(values[i].getAttribute("value"));	
    }
    return values;
}